import { ColumnsType } from "rc-table/es/interface";
import type { ChatTable } from "./type";

export function transColumns<RecordType>(
  columns: ChatTable.ColumnType<RecordType>[]
): any {
  return columns
    .filter((column: any) => column && typeof column === "object")
    .map((column, index) => {
      const { dataRowCol, ...resetColumn } = column;
      let onCell: any = undefined;
      if (dataRowCol) {
        onCell = (record: any, index: number) => {
          const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
          const findItem = dataRowCol.find((i: any) => i.index === index);
          if (findItem) {
            props.colSpan = findItem.colSpan;
            props.rowSpan = findItem.rowSpan;
          }
          return props;
        };
      }
      const subColumns = (column as ChatTable.ColumnGroupType<RecordType>)
        .children;
      if (subColumns && subColumns.length > 0) {
        return {
          ...resetColumn,
          onCell,
          children: transColumns(subColumns),
        };
      }
      return {
        ...resetColumn,
        onCell,
      };
    });
}
